/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo.conexion;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author carlos
 */
public class Conexion 
{
    private Connection  conexion =null;
    
    private Statement s;
    private ResultSet rs;

    public Conexion() {
        
    }
    
   
    
    public void conectar(){
        try {
            
            Class.forName("com.mysql.jdbc.Driver");
            conexion=  (Connection) DriverManager.getConnection ("jdbc:mysql://localhost/subasta","root", "1234");
            System.out.println("conexion ok");
            //JOptionPane.showMessageDialog(null,"conectado");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            
        }
        
    }
    
    public void desconectar()
    {
        try {
            conexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //TE DEVUELVE datos de la bases
    public ResultSet ejecutarConsulta(String consulta)
    {
        conectar();
        try {
            s = conexion.createStatement();
            rs = s.executeQuery (consulta);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        desconectar();
        return null;
    }
    
    //ejecutar update,insert,delete
     public void ejecutarQuery(String consulta)
    {
        conectar();
        try {
            s = conexion.createStatement();
            s.execute(consulta);
            
           
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        desconectar();
        
    }
    
     public static void main(String[] args) {
         Conexion conexion=new Conexion();
         conexion.conectar(); 
     }

}
